package com.netposa.datawarehouse.bodysearch.config;

import com.netposa.datawarehouse.bodysearch.convert.DeepLDictionaryConvertImpl;
import com.netposa.datawarehouse.bodysearch.convert.DictionaryConvert;
import com.netposa.datawarehouse.bodysearch.convert.PccDictionaryConvertImpl;
import com.netposa.datawarehouse.bodysearch.service.RequestIdService;
import com.netposa.library.api.IDictionaryCache;
import com.netposa.vdt.pccclient.autoconfigure.StructureTaskConfigProperties;
import java.net.UnknownHostException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

@Configuration
public class AutoConfiguration {
   private static final Logger log = LoggerFactory.getLogger(AutoConfiguration.class);
   @Autowired
   private IDictionaryCache dictionaryCache;
   @Autowired
   private StructureTaskConfigProperties properties;

   @Bean({"requestAssociateValueRedisTemplate"})
   public RedisTemplate requestAssociateValueRedisTemplate(RedisConnectionFactory redisConnectionFactory) throws UnknownHostException {
      RedisTemplate template = new RedisTemplate();
      template.setConnectionFactory(redisConnectionFactory);
      template.setKeySerializer(new StringRedisSerializer());
      template.setValueSerializer(new Jackson2JsonRedisSerializer(RequestIdService.RequestAssociateValue.class));
      return template;
   }

   @Bean(
      name = {"searchDictionaryConvert"}
   )
   public DictionaryConvert dictionaryConvert() {
      log.info("Dictionary convert use algorithm dictionary is {}.", this.properties.getStructureBodyType());
      if (this.properties.getStructureBodyType().equals("PCC")) {
         return new PccDictionaryConvertImpl(this.dictionaryCache);
      } else if (this.properties.getStructureBodyType().equals("DEEPLINT")) {
         return new DeepLDictionaryConvertImpl(this.dictionaryCache);
      } else if (this.properties.getStructureBodyType().equals("DEEPLINTXAPIV1")) {
         return new DeepLDictionaryConvertImpl(this.dictionaryCache);
      } else {
         return this.properties.getStructureBodyType().equals("HIKVISION") ? null : null;
      }
   }
}
